package com.bawei.service.impl;

import com.bawei.mapper.ExamineMapper;
import com.bawei.mapper.OvertimeMapper;
import com.bawei.mapper.UserInfoMapper;
import com.bawei.pojo.*;
import com.bawei.service.OvertimeService;
import com.bawei.util.PageView;
import com.bawei.util.ResultEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OvertimeServiceImpl implements OvertimeService {

    @Autowired
    private OvertimeMapper overtimeMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ExamineMapper examineMapper;

    @Override
    public PageView<Overtime> overtimeList(String currentPage, List<UserInfo> userInfoList) {
        List<Overtime> list = null;
        PageHelper.startPage(Integer.parseInt(currentPage), 3);
        OvertimeExample example = new OvertimeExample();
        OvertimeExample.Criteria criteria = example.createCriteria();
        if (userInfoList != null) {
            for (UserInfo userInfo : userInfoList) {
                criteria.andUserIdEqualTo(userInfo.getId());
                list = overtimeMapper.selectByExample(example);
            }
        } else {
            list = overtimeMapper.selectByExample(null);
        }
        PageInfo<Overtime> pageInfo = new PageInfo<>(list);
        PageView<Overtime> pageView = new PageView<>();
        pageView.setTotalPage(pageInfo.getTotal());
        pageView.setTotalRecord(pageInfo.getTotal());
        pageView.setRecords(list);
        pageView.setCurrentPage(Integer.parseInt(currentPage));
        return pageView;
    }

    @Override
    public ResultEntity add(String startTime, String endTime, String des, String uid) {
        Overtime overtime = this.checkOverTime(null, startTime, endTime, des);
        overtime.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        overtime.setStatus(0);//未审核
        int count = overtimeMapper.insertSelective(overtime);
        Examine examine = new Examine();
        examine.setId(UUID.randomUUID().toString());
        examine.setStatus(0);//审核表
        examine.settId(overtime.getId());
        examine.settType("加班管理");
        examineMapper.insertSelective(examine);
        if (count > 0) {
            return new ResultEntity("200", "加班提交成功");
        }
        return new ResultEntity("400", "系统错误");
    }

    @Override
    public List<UserInfo> RealNameLike(String realName) {
        UserInfoExample example = new UserInfoExample();
        UserInfoExample.Criteria criteria = example.createCriteria();
        criteria.andRealNameLike("%" + realName + "%");
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(example);
        if (!userInfoList.isEmpty() && userInfoList.size() != 0) {
            return null;
        }
        return userInfoList;
    }

    @Override
    public ResultEntity update(String id, String startTime, String endTime, String des) {
        Overtime userInfo = overtimeMapper.selectByPrimaryKey(id);
        //审核通过
        if(userInfo.getStatus().equals(1)){
            return new ResultEntity("400", "已经审核通过不能进行修改");
        }
        Overtime overtime = this.checkOverTime(id, startTime, endTime, des);
        int count = overtimeMapper.updateByPrimaryKeySelective(overtime);
        if (count > 0) {
            return new ResultEntity("200", "加班修改成功");
        }
        return new ResultEntity("400", "系统错误");
    }

    @Override
    public Overtime detail(String id) {
        return overtimeMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultEntity delete(String ids) {
        if (ids != null) {
            String[] newIds = ids.split(",");
            for (String newId : newIds) {
                overtimeMapper.deleteByPrimaryKey(newId);
            }
            return new ResultEntity("200", "加班删除成功");
        }
        return new ResultEntity("400", "系统错误");
    }

    /**
     * 封装添加  修改通用时间操作
     * @param id   主键
     * @param startTime  开始时间
     * @param endTime  结束时间
     * @param des   描述
     * @return
     */
    private Overtime checkOverTime(String id, String startTime, String endTime, String des) {
        Overtime overtime = new Overtime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = format.parse(startTime);
            Date endDate = format.parse(endTime);
            long time = startDate.getTime();
            long time1 = endDate.getTime();
            String l = (time1 - time) / 3600000 + "";
            overtime.setDuration(Integer.parseInt(l));
            overtime.setStartTime(startDate);
            overtime.setEndTime(endDate);
            Date date = new Date();
            String newApplicationDate = format.format(date);
            overtime.setApplicationTime(format.parse(newApplicationDate));
            overtime.setDes(des);
            overtime.setId(id);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return overtime;
    }
}
